package designpatterns.proxy;

import designpatterns.proxy.cglibproxy.CglibUserServiceProxy;
import designpatterns.proxy.dynamicproxy.DynamicUserServiceProxy;
import designpatterns.proxy.dynamicproxy.UserServiceImpl;
import designpatterns.proxy.staticproxy.UserServiceProxyImpl;
import org.junit.Test;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2021/3/29
 * @Version：1.0.0
 */
public class AppTest {

    /**
     * 静态代理
     */
    @Test
    public void test1() {
        UserService userService = new UserServiceImpl();
        UserServiceProxyImpl userServiceProxy = new UserServiceProxyImpl(userService);
        // 用代理类执行 UserServiceImpl 中test1()
        userServiceProxy.methodTest();
    }

    /**
     * JDK 动态代理
     */
    @Test
    public void test2() {
        UserService userService = new UserServiceImpl();
        DynamicUserServiceProxy dynamicUserServiceProxy = new DynamicUserServiceProxy(userService);
        UserService userService1 = (UserService) dynamicUserServiceProxy.getProxyInstance();
        userService1.methodTest();
    }

    /**
     * CGlib 动态代理
     */
    @Test
    public void tes3() {
        //目标对象
        UserService userService = new UserServiceImpl();
        //代理对象
        UserService proxy = (UserService) new CglibUserServiceProxy(userService).getProxyInstance();
        //执行代理对象方法
        proxy.methodTest();
    }

}
